package com.dinnovate.dxf.service;

import com.dinnovate.dxf.analysis.DxfAnalysis;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

import java.util.List;


public class dxfDataService {


    /**
     * 处理dxf文件
     *
     * @param fileUrl dxf文件
     * @param noText  导出文件名
     * @param outUrl  选择输出路径
     */
    public void executionDXF(String fileUrl, String noText, String outUrl) throws IOException {
        File file = new File(fileUrl);
//        InputStream inputStream = Files.newInputStream(file.toPath());
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        List<List<String>> pointNumList = DxfAnalysis.getGeometricListMap(reader);

        //-------------------打印所有信息
/*        List<List<String>> pointNumList = new ArrayList<>();
        List<String> pointList = new ArrayList<>();
        List<String> lineList = readAllLine(reader);
        for (String pointNum : lineList) {
            pointList.add(pointNum);
            pointNumList.add(pointList);
            pointList = new ArrayList<>();
        }
        show(pointNumList, outUrl, noText);*/
        //-------------------
        show(pointNumList, outUrl, noText);
        //关闭文件流
        inputStream.close();
        reader.close();
    }


    /**
     * 读取所有的行
     *
     * @param reader BufferedReader
     * @return 返回文件的所有数据，以每一行数据为一个item
     * @throws IOException IO异常
     */
    public static List<String> readAllLine(BufferedReader reader) throws IOException {
        List<String> list = Lists.newArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            list.add(line.trim());
        }
        return list;
    }

    /**
     * 创建文档
     *
     * @param pointNumList 建表数据
     * @param outUrl       输出地址
     * @param noText       文件名
     */
    public static void show(List<List<String>> pointNumList, String outUrl, String noText) {

        String outFileUrl = outUrl + "\\" + noText + "点位数据表.xlsx";

        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("读取dxf数据文件");//创建一张表
        int cell = 0;

        for (List<String> pointList : pointNumList) {//遍历每一个节点
            int rowNum = 0;
            Row row = sheet.createRow(cell);//从第一行开始保存数据
            for (String point : pointList) {//遍历每一个节点内的属性
                row.createCell(rowNum).setCellValue(point);
                rowNum += 1;
            }
            cell += 1;
        }

        try {
            File outPutFile = new File(outFileUrl);//输出文档
            //保存文档
            FileOutputStream outputStream = new FileOutputStream(outPutFile);//文件路径
            wb.write(outputStream);
            outputStream.close();
//            System.out.println("创建完成");
            JOptionPane.showMessageDialog(null, "创建完成");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "创建失败");
        }
    }


}
